import axios from 'axios';
import router from '../router';
import store from '../store'
axios.defaults.baseURL = '/api'
// 前端拦截器
axios.interceptors.request.use(config=>{
    const token = store.state.user.token; // vuex中取出token
    if(token) {
        config.headers.authorization = "Bearer " +token
    }
    return {...config}
})
// 响应头拦截器
axios.interceptors.response.use(response=>{
    return response.data
},err=>{
    if(err.response){
        switch(err.response.status) {
            case 401:
                router.push('/login') 
            break;
            case 404:
                alert('接口未找到')
            break;
            // 500
        }
    }
    
    return Promise.reject(err)
})

export default axios